package com.controller.front;

import com.entity.Order;
import com.entity.User;
import com.service.OrderService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.vo.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("front/order")
public class OrderFrontController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("addOrder")
    public String addOrder(Order order, HttpSession session){
        System.out.println("order = " + order);
        User user = (User)session.getAttribute("user");
        order.setUser_id(user.getUser_id());
        order.setStatus("未支付");
        order.setCreate_date(new Date());
        System.out.println("order_id = " + order.getOrder_id());
        Cart cart = (Cart)session.getAttribute("cart");
        orderService.insertOrder(order,cart.getItems().values());
        cart.getItems().clear();

        return "redirect:/front/order/showOneOrder.do?id="+order.getOrder_id();
    }
    @RequestMapping("showOneOrder")
    public String showOneOrder(Integer id, Model model){
        Order order = orderService.selectOrderById(id);
        System.out.println("order = " + order);
        model.addAttribute("order",order);
        return "forward:/front/pay/pay_order.jsp";
    }
    @RequestMapping("updateOrder")
    public String updateOrder(Integer id){
        Order order = orderService.selectOrderById(id);
        order.setStatus("已支付");
        orderService.updateOrder(order);
        return "redirect:/front/main.do";
    }
}
